<template>
    <div class="login-div">
        <h2 class="login-title">教学过程管理系统</h2>
        <div class="form-class">
            <el-form
                ref="ruleFormRef"
                :model="ruleForm"
                :rules="rules"
                label-width="0"
                class="demo-ruleForm"
            >
                <el-form-item id="uid" label="UID" prop="uid" label-width="50">
                    <el-input style="z-index: 10;" v-model="ruleForm.uid"/>
                </el-form-item>
                <el-form-item id="password" label="Password" prop="password" label-width="50">
                    <el-input style="z-index: 10;" v-model="ruleForm.password" type="password" autocomplete="off"/>
                </el-form-item>
                <el-form-item>
                    <div style="display: flex;flex-direction: row;justify-content: space-between">
                        <el-button style="margin-left: 40px" type="primary" @click="submitForm(ruleFormRef)">登录
                        </el-button>
                        <el-button style="margin-left: 40px" type="warning" @click="resetForm(ruleFormRef)">清空
                        </el-button>
                    </div>
                </el-form-item>
            </el-form>
        </div>
    </div>
    <div class="login-bg">
        <img src="../../assets/images/login-bg.png" alt="">
    </div>
    <div class="uid-shadow"></div>
    <div class="password-shadow"></div>
    <div class="login-div-shadow"></div>
</template>

<script lang="ts" setup>
import {reactive, ref} from 'vue'
import type {ComponentSize, FormInstance, FormRules} from 'element-plus';
import {loginUser} from '@/service/user.service'

const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()

// 表单对象
interface RuleForm {
    uid: string
    password: string
}

const ruleForm = reactive<RuleForm>({
    uid: '',
    password: '',
})

// 表单规则
const rules = reactive<FormRules<RuleForm>>({
    uid: [
        {required: true, message: '用户昵称不能为空', trigger: 'blur'},
    ],
    password: [
        {required: true, message: '密码不能为空', trigger: 'blur'},
        {min: 6, max: 28, message: '密码长度至少为6个字符，最多为28个字符', trigger: 'blur'},
    ],
    
})

const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            console.log('submit!')
            loginUser(ruleForm)
        } else {
            console.log('error submit!', fields)
        }
    })
}

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
}
</script>

<style scoped>
.login-bg {
    top: 0;
    left: 0;
    position: fixed;
    display: block;
    width: 100%;
    height: 100vh;
    z-index: -2;
    
    img {
        width: 650px;
        z-index: 999;
        position: relative;
        top: 150px;
        left: 80px;
    }
}

.login-bg::after {
    display: block;
    position: fixed;
    content: "";
    justify-content: center;
    background-color: #a0fab4;
    width: 110%;
    height: 230%;
    border-radius: 50%;
    top: -65%;
    left: -50%;
}

.login-div {
    position: relative;
    place-content: center;
    display: flex;
    flex-direction: column;
    background-color: #ffffff;
    margin: 0;
    border-radius: 20px;
    border: 2px solid #f2f2f2;
    box-shadow: 2px 2px 5px 0 rgba(0, 0, 0, 0.2);
    width: 500px;
    top: 207px;
    left: 930px;
}

.login-div-shadow {
    content: "";
    width: 500px;
    height: 265px;
    background-color: rgba(0, 0, 0, 0.2);
    position: relative;
    display: block;
    top: -100px;
    left: 950px;
    border-radius: 20px;
    z-index: -1;
}

.login-title {
    text-align: center;
    display: block;
    font-size: 40px;
    font-weight: bold;
    color: #14b2f5;
}

.form-class {
    width: 60%;
    margin: 40px auto 0 auto;
}

.uid-shadow {
    display: block;
    position: relative;
    top: 65px;
    left: 1110px;
    content: "";
    height: 30px;
    width: 200px;
    background-color: #000000;
    background-color: rgba(0, 0, 0, 0.3);
    border-radius: 5px;
    z-index: 9;
}

.password-shadow {
    display: block;
    position: relative;
    top: 87px;
    left: 1110px;
    content: "";
    height: 30px;
    width: 200px;
    background-color: #000000;
    background-color: rgba(0, 0, 0, 0.3);
    border-radius: 5px;
    z-index: 9;
}

.el-form {
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.el-form-item {
    margin: 10px auto;
    justify-content: center;
}
</style>